<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <a-form-item :label="$t('订单时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="5" :md="6" :sm="24">
            <a-form-item :label="$t('仓库')">
              <a-tree-select
                allowClear
                v-model="queryParam.store"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="storeList"
                :search-placeholder="$t('请选择')"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="5" :md="6" :sm="24">
            <a-form-item :label="$t('订单状态')">
              <a-select :placeholder="$t('请选择订单状态')" v-model="queryParam.state">
                <a-select-option v-for="(item, index) in stateList" :value="item.value" :key="index">{{
                  item.title
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('订单号')">
                <a-input :placeholder="$t('请输入订单号')" v-model="queryParam.id"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('审核人')">
                <a-select v-model="queryParam.audit">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="handleReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'BisGoodsBackController:add'">{{ $t('新增') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.state != '1'" v-has="'BisGoodsBackController:edit'">{{ $t('编辑') }}</a>
          <a-popconfirm :title="$t('您确定要删除该订单吗?')" @confirm="() => handleDelete(record.id)">
            <a v-if="record.state != '1'" style="margin-left: 8px" v-has="'BisGoodsBackController:delete'">{{ $t('删除') }}</a>
          </a-popconfirm>
          <a @click="handleDetail(record, 1)" style="margin-left: 8px">{{ $t('详情') }}</a>
          <a v-if="record.state != '1'" @click="handleDetail(record, 2)" style="margin-left: 8px" v-has="'BisGoodsBackController:edit'">{{ $t('审核') }}</a>
        </span>
      </a-table>
    </div>
    <!-- <return-goods-modal ref="modalForm" @ok="modalFormOk"></return-goods-modal> -->
    <return-goods-standby-modal ref="modalForm" @ok="modalFormOk"></return-goods-standby-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import ReturnGoodsModal from './modules/ReturnGoodsModal'
import ReturnGoodsStandbyModal from './modules/ReturnGoodsStandbyModal'

export default {
  name: 'ReturnGoodsList',
  mixins: [JeecgListMixin],
  components: {
    ReturnGoodsModal,
    ReturnGoodsStandbyModal,
  },
  data() {
    return {
      moment,
      description: this.$t('商品返货'),
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: this.$t('创建时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: this.$t('仓库'),
          align: 'center',
          dataIndex: 'store_dictText',
        },
        {
          title: this.$t('订单状态'),
          align: 'center',
          dataIndex: 'state_dictText',
          customCell: function (record, text) {
            if (record.state == 0) {
              return {
                style: {
                  color: '#EF2315',
                },
              }
            }
          },
        },
        {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment',
        },
        {
          title: this.$t('审核时间'),
          align: 'center',
          dataIndex: 'auditTime',
        },
        {
          title: this.$t('审核人'),
          align: 'center',
          dataIndex: 'audit',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/goods/bisGoodsBack/list',
        delete: '/goods/bisGoodsBack/delete',
      },
      dictOptions: {},
      superFieldList: [],
      storeList: [],
      stateList: [],
      userList: [],
      queryParam: {
        store: '',
        state: '',
        inType: '',
        createBy: '',
        audit: '',
      },
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.getStoreList()
      initDictOptions('bis_goods_order.state').then((res) => {
        if (res.success) {
          this.stateList = res.result
          this.stateList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      getAction('/sys/user/list').then((res) => {
        if (res.success) {
          this.userList = res.result.records
          this.userList.forEach((col) => {
            col.id = col.realname
          })
          this.userList.unshift({ id: '', realname: this.$t('全部') })
        }
      })
    },
    getStoreList() {
      let that = this
      let storeList = []
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'id'
      param.order = 'DESC'
      getAction('/goods/bisGoodsStore/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          storeList = res.result.records
          if (storeList.length > 0) {
            storeList.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            that.storeList = that.getDataByResult(storeList)
            that.storeList.unshift({ key: '', id: '', value:'', title: that.$t('全部') })
          }
        }
      })
    },
    getHasChildStore(store) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = store.id
      getAction('/goods/bisGoodsStore/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            store.children = that.getDataByResult(res.result.records)
            that.storeList = [...that.storeList]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    handleAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = this.$t('新增返货单')
      this.$refs.modalForm.disableSubmit = false
    },
    handleDetail(record, state) {
      this.$refs.modalForm.edit(record, state)
      if (state == 1) {
        this.$refs.modalForm.title = this.$t('详情')
      } else {
        this.$refs.modalForm.title = this.$t('审核')
      }
      this.$refs.modalForm.disableSubmit = true
    },
    handleReset() {
      this.queryParam = {
        store: '',
        state: '',
        inType: '',
        createBy: '',
        audit: '',
      }
      this.loadData(1)
    },
    modalFormOk() {
      this.loadData(1)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>